package com.test.utils;

import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson2.JSONObject;
import com.test.enums.ActionType;
import com.test.vo.RequestVo;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * 返回结果处理
 *
 * @author sangkuofeng
 * @date 2024/3/24 11:32
 */
public class SocketResponseUtils {

    private static final String IDCARDSTR = FileUtil.readString("/Users/sangkuofeng/Documents/IdeaProjects/test-socket/src/test/resources/cai.json", StandardCharsets.UTF_8);


    /**
     * 计算返回结果
     *
     * @param reqStr
     * @return
     */
    public static String getReturnResult(String reqStr) {
        RequestVo requestVo = JSONObject.parseObject(reqStr, RequestVo.class);
        if (StringUtils.equalsIgnoreCase(requestVo.getAction(), ActionType.READ_CARD_NUMBER.getCode())) {
            return readIcCardNo(requestVo.getRequestId(), requestVo.getDeviceId());
        } else if (StringUtils.equalsIgnoreCase(requestVo.getAction(), ActionType.WRITE_CARD_NUMBER.getCode())) {
            return writeIcCardNo(requestVo.getRequestId(), requestVo.getParams());
        } else if (StringUtils.equalsIgnoreCase(requestVo.getAction(), ActionType.READ_STRING.getCode())) {
            return readData(requestVo.getRequestId(), requestVo.getDeviceId());
        } else if (StringUtils.equalsIgnoreCase(requestVo.getAction(), ActionType.WRITE_STRING.getCode())) {
            return writeData(requestVo.getRequestId(), requestVo.getParams());
        } else if (StringUtils.equalsIgnoreCase(requestVo.getAction(), ActionType.READ.getCode())) {
            return readIdCardNo(requestVo.getRequestId(), requestVo.getDeviceId());
        }
        return null;
    }


    /**
     * 读卡号
     *
     * @param requestId
     * @param deviceId
     * @return
     */
    public static String readIcCardNo(String requestId, String deviceId) {
        JSONObject result = new JSONObject();
//       {"code":"1","data":"LS007","requestId":"TEST01"}
        result.put("code", "1");
        result.put("data", "LS007");
        result.put("requestId", requestId);
        return JSONObject.toJSONString(result);
    }

    /**
     * 写卡号
     *
     * @param requestId
     * @param params
     * @return
     */
    public static String writeIcCardNo(String requestId, Map<String, String> params) {
        JSONObject result = new JSONObject();
//        {"code":"0","data":"写卡失败","requestId":"TEST01"}
        result.put("code", "1");
        result.put("data", "写卡成功");
        result.put("requestId", requestId);
        return JSONObject.toJSONString(result);
    }

    /**
     * 读数据
     *
     * @param requestId
     * @param deviceId
     * @return
     */
    public static String readData(String requestId, String deviceId) {
        JSONObject result = new JSONObject();
//      {"code":"1","data":"测试1234","requestId":"TEST01"}
        result.put("code", "1");
        result.put("data", "测试1234");
        result.put("requestId", requestId);
        return JSONObject.toJSONString(result);
    }

    /**
     * 写数据
     *
     * @param requestId
     * @param params
     * @return
     */
    public static String writeData(String requestId, Map<String, String> params) {
        JSONObject result = new JSONObject();
//        {"code":"0","data":"写卡失败","requestId":"TEST01"}
        result.put("code", "1");
        result.put("data", "写卡成功");
        result.put("requestId", requestId);
        return JSONObject.toJSONString(result);
    }

    /**
     * 蜂鸣
     *
     * @param requestId
     * @param params
     * @return
     */
    public static String beep(String requestId, Map<String, String> params) {
        JSONObject result = new JSONObject();
//        {"code":"1","data":"蜂鸣成功","requestId":"TEST01"}
        result.put("code", "1");
        result.put("data", "蜂鸣成功");
        result.put("requestId", requestId);
        return JSONObject.toJSONString(result);
    }

    /**
     * 读身份证
     *
     * @param requestId
     * @param deviceId
     * @return
     */
    public static String readIdCardNo(String requestId, String deviceId) {
        JSONObject result = new JSONObject();
//
        result.put("code", "1");
        JSONObject data = new JSONObject();

        result.put("data", "测试1234");
        result.put("requestId", requestId);
//        if (count < 2) {
//            return "{\"code\":\"0\",\"data\":\"no card\",\"requestId\":\"" + requestId + "\"}";
//        } else {
//            count = 0;
        String resultStr = "{\"code\":\"1\",\"data\":{\"data\":{\"address\":\"江苏省无锡市滨湖区水秀新村84号602室\",\"birthday\":\"1984-02-02\",\"expireDate\":\"20360905\",\"gender\":\"男\",\"genderGB\":null,\"id\":\"320211198402024517\",\"issueDate\":\"20160905\",\"issuer\":null,\"name\":\"陆昱相\",\"nation\":\"汉\",\"nationGB\":null,\"photo\":\"/9j/4AAQ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nOQcyMMc1sbRtzXGeJnMs5jb7oBxUxRUjkULSTvK33nOTVoDmsyW/W1IDIWPbFWLLUFvOQhUjsa9CmvdOGb94m5WVl7Z4p4OaScYkBHcUi1SJZYjfYDjvRUVFMR//2Q==\"},\"type\":\"IDCard\"},\"requestId\":\"" + requestId + "\"}";
        return new String(resultStr.getBytes(StandardCharsets.UTF_8));
//        }
    }


    /**
     * 地磅称重
     *
     * @param requestId
     * @return
     */
    public static String subscribe(String requestId) {
        JSONObject result = new JSONObject();
        result.put("code", "1");
        result.put("data", "15");
        result.put("requestId", requestId);
        return JSONObject.toJSONString(result) + "@";
    }

}
